/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 模板
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_Template")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Template implements Comparable<Object>, Serializable {
	
	private static final long serialVersionUID = -7101194587968978390L;

	@Id
	@Column(name="Template_GUID",length=38)
	@GenericGenerator(name="Template-assigned",strategy="assigned")
	private String guid;
	
	@Column(name="Template_Code",length=40,nullable=false)
	private String code;
	
	@Column(name="Template_Name",length=100)
	private String name;
	
	@Column(name="Template_Description",length=200)
	private String description;
	
	@Column(name="isDebug")
	private Boolean debug;
	
	@Column(name="template_inner")
	private Boolean inner;
	
	@Column(name="template_isbaseinfo")
	private Boolean baseInfo;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="ReportPeriod_Type_GUID")
	private ReportPeriodType reportPeriodType;
	
	@Basic
	private Double tabIndex;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="App_GUID",nullable=true)
	private Application application;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="TemplateFile_GUID",nullable=true)
	private TemplateFile templateFile;
	
	@Column(name="Template_usage")
	private Integer usage;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="template")
	@OrderBy("tabIndex")
	private List<TemplateIndication> templateIndications;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="template")
	@OrderBy("tabIndex")
	private List<TemplateRange> templateRanges;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="template")
	@OrderBy("tabIndex")
	private List<Formula> formulas;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="template")
	@OrderBy("tabIndex")
	private List<Validation> validations;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="template")
	@OrderBy("tabIndex")
	private List<TemplateDependence> dependences;
	
	@Transient
	public String getReportPeriodTypeGuid() {
		if(reportPeriodType != null){
			return reportPeriodType.getGuid();
		}
		return "";
	}
	
	public void setReportPeriodTypeGuid(String reportPeriodTypeGuid) {
		if(reportPeriodTypeGuid != null && reportPeriodTypeGuid.trim().length()>0){
			if(reportPeriodType==null){
				reportPeriodType = new ReportPeriodType();				
			}
			reportPeriodType.setGuid(reportPeriodTypeGuid);			
		}
	}
	
	public List<TemplateDependence> getDependences() {
		return dependences;
	}

	public void setDependences(List<TemplateDependence> dependences) {
		this.dependences = dependences;
	}
	
	public Boolean getInner() {
		return inner;
	}
	public void setInner(Boolean inner) {
		this.inner = inner;
	}
	
	public Boolean getBaseInfo() {
		return baseInfo;
	}
	public void setBaseInfo(Boolean baseInfo) {
		this.baseInfo = baseInfo;
	}
	
	public List<Validation> getValidations() {
		return validations;
	}

	public void setValidations(List<Validation> validations) {
		this.validations = validations;
	}

	public List<Formula> getFormulas() {
		return formulas;
	}

	public void setFormulas(List<Formula> formulas) {
		this.formulas = formulas;
	}

	public List<TemplateIndication> getTemplateIndications() {
		return templateIndications;
	}

	public void setTemplateIndications(List<TemplateIndication> templateIndications) {
		this.templateIndications = templateIndications;
	}

	public List<TemplateRange> getTemplateRanges() {
		return templateRanges;
	}

	public void setTemplateRanges(List<TemplateRange> templateRanges) {
		this.templateRanges = templateRanges;
	}

	public Integer getUsage() {
		return usage;
	}

	public void setUsage(Integer usage) {
		this.usage = usage;
	}

	public TemplateFile getTemplateFile() {
		return templateFile;
	}

	public void setTemplateFile(TemplateFile templateFile) {
		this.templateFile = templateFile;
	}

	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public Boolean getDebug() {
		return debug;
	}

	public void setDebug(Boolean debug) {
		this.debug = debug;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ReportPeriodType getReportPeriodType() {
		return reportPeriodType;
	}

	public void setReportPeriodType(ReportPeriodType reportPeriodType) {
		this.reportPeriodType = reportPeriodType;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Template myClass = (Template) object;
		return new CompareToBuilder().append(this.code, myClass.code).append(
				this.debug, myClass.debug).append(this.description, myClass.description).append(this.usage, myClass.usage).append(this.guid, myClass.guid).append(this.name, myClass.name).append(this.tabIndex, myClass.tabIndex).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Template)) {
			return false;
		}
		Template rhs = (Template) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.code, rhs.code)
				.append(this.debug, rhs.debug).append(this.description, rhs.description).append(this.usage, rhs.usage).append(this.guid, rhs.guid).append(this.name, rhs.name).append(this.tabIndex, rhs.tabIndex)
				.isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(1736088039, -881227817).appendSuper(super.hashCode()).append(this.code).append(this.debug).append(this.description).append(
				this.usage).append(this.guid).append(this.name).append(this.tabIndex).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("usage", this.usage).append("tabIndex", this.tabIndex).append("name", this.name).append("description", this.description).append("code", this.code).append("debug", this.debug).append("guid", this.guid).toString();
	}
}